//
//  PictureListViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/3/7.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "PictureListViewController.h"
#import "PictureThinkViewController.h"

@interface PictureListViewController ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIScrollView *scrollView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation PictureListViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"涂鸦篇";
    UIImage *image = [UIImage imageNamed:@"mantuoluoWhite_bg"];
    self.view.layer.contents = (id) image.CGImage;
    
    [self creatDate];
    [self creatScrollView];
    
}
- (void)creatScrollView{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    scrollView.pagingEnabled=NO;
    scrollView.backgroundColor=[UIColor whiteColor];
    scrollView.delegate=self;
    
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame = CGRectMake((kScreenW - 60)/2, 20, 60, 60);
    titleLabel.text = @"静";
    titleLabel.font = [UIFont systemFontOfSize:25];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = KBACKNAVIGATIONCOLOR;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.clipsToBounds = YES;
    titleLabel.layer.cornerRadius = 30;
    [self.scrollView addSubview:titleLabel];
    
    
    
}

- (void)creatDate{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.pictureListId forKey:@"id"]; //关卡id
    [yjNetWorkManger requestType:GET andURL:[JSPathUtil urlAppInterestGetGuanQiaById] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            MyLog(@"%@",response);
            self.dataArray = [NSMutableArray arrayWithArray:response[@"manDitu"]];
//            NSMutableArray *array = [NSMutableArray arrayWithArray:response[@"manDitu"]];
//            [array addObjectsFromArray:response[@"manDitu"]];
//            [array addObjectsFromArray:response[@"manDitu"]];
//            [array addObjectsFromArray:response[@"manDitu"]];
            UILabel *contentLabel = [[UILabel alloc]init];
            contentLabel.text = @"慢慢调整自己的心境，深深呼吸，将注意力集中在自己的呼吸上，在呼吸中让自己沉静下来。如果喜欢，可以采用静坐或听轻音乐的方式，让浮躁的意识平静下来。此刻，你的心如明镜，一切静待呈现";
            contentLabel.font = [UIFont systemFontOfSize:16];
            contentLabel.frame = CGRectMake(20, 100, kScreenW - 40, [Global getSizeOfString:contentLabel.text maxWidth:kScreenW - 40 maxHeight:MAXFLOAT withFontSize:16.6].height);
            contentLabel.numberOfLines = 0;
            contentLabel.textColor = KTEXTMAINCOLOR;
            [self.scrollView addSubview:contentLabel];
            
            self.scrollView.contentSize=CGSizeMake(0, CGRectGetMaxY(contentLabel.frame)+ 20 + (self.dataArray.count/2)*(kScreenW/2-40+10));
            
            for (int i = 0; i < self.dataArray.count; i++) {
                UIImageView *imageView = [[UIImageView alloc]init];
                imageView.frame = CGRectMake( 20 + (i%2)*(kScreenW/2), CGRectGetMaxY(contentLabel.frame)+ 20 + (i/2)*(kScreenW/2-40 + 10), kScreenW/2- 40, kScreenW/2-40);
                if (!strIsEmpty(self.dataArray[i][@"img"])) {
                    [imageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[i][@"img"]] placeholderImage:KRECOMMENDLISTDEFAULT];
                    imageView.userInteractionEnabled = YES;
                    imageView.tag = 1000+i;
                    [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterImage:)]];
                }
                MyLog(@"%f %f",imageView.frame.origin.y,imageView.frame.size.height);
                [self.scrollView addSubview:imageView];
            }
            
        }
    }];
}
#pragma mark - 点击图片进入详情页
- (void)enterImage:(UIGestureRecognizer *)sender {
    PictureThinkViewController *pictureThinkVC = [[PictureThinkViewController alloc]init];
    pictureThinkVC.pictureId = self.dataArray[sender.view.tag - 1000][@"id"];
    pictureThinkVC.imageUrl = self.dataArray[sender.view.tag - 1000][@"img"];
    [self.navigationController pushViewController:pictureThinkVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
